package com.joizhang.seckill.service;

import com.joizhang.seckill.api.redis.UserKey;
import com.joizhang.seckill.api.service.DemoService;
import com.joizhang.seckill.api.service.GoodsService;
import com.joizhang.seckill.api.service.UserService;
import com.joizhang.seckill.api.vo.UserVo;
import com.joizhang.seckill.kafka.MQSender;
import com.joizhang.seckill.model.User;
import com.joizhang.seckill.redis.RedisService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@DubboService
@Service
public class DemoServiceImpl implements DemoService {

    private final RedisService redisService;

    private final MQSender sender;

    private final UserService userService;

    private final GoodsService goodsService;

    @Autowired
    public DemoServiceImpl(RedisService redisService, MQSender sender, UserService userService, GoodsService goodsService) {
        this.redisService = redisService;
        this.sender = sender;
        this.userService = userService;
        this.goodsService = goodsService;
    }


    @Override
    public void doubleInsert() {

    }

    @Override
    public UserVo getUserById(Long id) {
        return userService.getUserById(id);
    }

    @Override
    public Boolean setRedis(UserKey userKey, String key, UserVo userVo) {
        User user = new User();
        BeanUtils.copyProperties(userVo, user);
        return redisService.set(userKey, key, user);
    }

    @Override
    public UserVo getRedis(UserKey userKey, String key, Class<UserVo> userVoClass) {
        return (UserVo) redisService.get(userKey, key);
    }

    @Override
    public Boolean initRedis() {
        return goodsService.initRedis();
    }

    @Override
    public void sendToMQ(String message) {
        sender.send(message);
    }
}
